﻿using System;
using System.Text;

/// <summary>
/// 是个单个缓存. 始终缓存一个StringBuilder
/// </summary>
public class SDStringBuilder {
    
    [ThreadStatic]
    private static StringBuilder m_Instance = new StringBuilder();

    public static StringBuilder Create()
    {
        if (m_Instance!=null)
        {
            Clear();
            StringBuilder cache = m_Instance;
            m_Instance = null;
            return cache;
        }
        return new StringBuilder();
    }

    public static string ToStringAndRecycle(StringBuilder sb)
    {
        string s = sb.ToString();
        Recycle(sb);
        return s;
    }

    public static void Recycle(StringBuilder sb)
    {
        m_Instance = sb;
    }

    private static void Clear()
    {
        m_Instance.Length = 0;
    }
}
